package day16;

import Zhouliu.A;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class learn4 implements Runnable {


    public static void main(String[] args) throws IOException {
        //实例化对象
        learn4 learn4 = new learn4();
        //创建线程对象
        Thread thread = new Thread(learn4);
        //启动线程
        thread.start();


    }
    @Override
    public void run () {
        //序列化
        ObjectOutputStream O=null;
         Student stu=null;
         Teacher te=null;
        stu=new Student("朱玉","男");
       te=new Teacher("玉竹");
        //创建文件
        File file = new File("D:\\WUDownloadCache\\89.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream fileOutputStream = null;
        try {
            //输出流
            fileOutputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {

             O = new ObjectOutputStream(fileOutputStream);
             O.writeObject(stu);
            O.writeObject(te);
        } catch (IOException  e) {
            e.printStackTrace();
        }
        finally {
            if(O !=null){
                try {
                    O.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //反序列化
       ObjectInputStream objectInputStream=null;
        //输入流
        FileInputStream fileInputStream=null;
        try {
            fileInputStream=new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try{
            objectInputStream=new ObjectInputStream(fileInputStream);
            stu=(Student)objectInputStream.readObject();
            System.out.println("姓名为:" +stu.getName());
            System.out.println("性别为:"+stu.getSex());
            te=(Teacher)objectInputStream.readObject();
            System.out.println("姓名为:"+te.getName());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(objectInputStream!=null){
                try{
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ArrayList<Student>list=new ArrayList<Student>();
        list.add(stu);
        for(Student th:list){
            System.out.println("姓名为"+th.getName());
           System.out.println("性别为"+th.getSex());
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<Teacher>list1=new ArrayList<Teacher>();
        list1.add(te);
        for(Teacher te1:list1){
            System.out.println("姓名"+te1.getName());
        }

    }
    }

